package handler

import (
	"article/api/params"
	"article/golbal"
	"article/model"
	"github.com/gin-gonic/gin"
	"net/http"
	"path/filepath"
)

func Upload(c *gin.Context) {

	file, err := c.FormFile("image")
	if err != nil {
		return
	}
	//f := uuid.NewString() + ".png"
	//err = c.SaveUploadedFile(file, "./"+f)
	//if err != nil {
	//	return
	//}

	if file.Size > 10*1024*1024 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "图片大小不能超过10MB",
		})
	}

	ext := filepath.Ext(file.Filename)

	if ext != ".png" && ext != ".jpg" && ext != ".jpeg" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "图片格式不正确",
		})
	}

	//getwd, _ := os.Getwd()
	//
	////upload, err := pkg.Upload(getwd + "/" + f)
	////if err != nil {
	////	return
	////}
	//c.JSON(http.StatusBadRequest, gin.H{
	//	"code": 200,
	//	"msg":  "上传成功",
	//	"data": upload,
	//})
}

func WeiBoAdd(c *gin.Context) {

	req := params.WeiBoReq{}
	err := c.ShouldBind(&req)
	if err != nil {
		return
	}
	weibo := model.Weibo{

		UserId:  req.UserId,
		Image:   req.Image,
		Content: req.Content,
	}

	golbal.DB.Debug().Create(&weibo)

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "微博发布成功",
	})

}

func Show(c *gin.Context) {

	show := params.WeiBoReq{}
	err := c.ShouldBind(&show)
	if err != nil {
		return
	}
	page := show.Page
	if page <= 0 {
		page = 1
	}

	pageSize := show.Size
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	offset := (page - 1) * pageSize
	var weibo model.Weibo

	golbal.DB.Debug().Offset(offset).Limit(show.Size).Where("id=?", show.Id).Find(&weibo)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "查询成功",
	})

}
func Delete(c *gin.Context) {
	del := params.WeiBoReq{}
	err := c.ShouldBind(&del)
	if err != nil {
		return
	}

	var des model.Weibo
	golbal.DB.Debug().Where("id=?", des.ID).Delete(&model.Weibo{})

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})

}
